package uires
{
	import flash.net.URLRequest;
	import flash.system.ApplicationDomain;
	import flash.system.LoaderContext;
	import flash.utils.ByteArray;
	import flash.utils.Dictionary;

	public class swf_loader
	{
		private static var _resDic:Dictionary = new Dictionary();
		
		private static var _loaderDic:Dictionary = new Dictionary();
		
		private var _path:String;
		
		public function swf_loader()
		{
		}
		public function startLoad(path:String, backFun:Function):void
		{
			_path = path;
			
			if(!_loaderDic[path]){
				_loaderDic[path] = [];
			}
			if(_loaderDic[path].indexOf(backFun) == -1){
				_loaderDic[path].push(backFun);
			}
			
			if(_resDic[path]){
				exeBackFun();
			}
			else{
				var stream:LoginStreamLoader = new LoginStreamLoader(onStreamComplete);
//				stream.isMainSwf = true;
				stream.load(new URLRequest(path));
			}
		}
		private function onStreamComplete(stream:LoginStreamLoader):void 
		{
			var loader:LoginResLoader = new LoginResLoader(onComplete);
			
			var byteArray:ByteArray = stream.loadContent;
			
			var _loaderContext:LoaderContext = new LoaderContext(false, ApplicationDomain.currentDomain);
			_loaderContext.allowCodeImport = true;
			loader.loadBytes(byteArray, _loaderContext);
		}
		
		private function onComplete(loader:LoginResLoader):void {
			_resDic[_path] = loader.content;
			exeBackFun();
		}
		private function exeBackFun():void
		{
			if(_resDic[_path]){
				var arr:Array = _loaderDic[_path];
				if(arr){
					for(var i:int=0; i<arr.length; i++){
						arr[i](_resDic[_path]);
					}
					delete _loaderDic[_path];
				}
			}
		}
	}
}